package com.dims_hsrc.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dims_hsrc.common.annotation.Log;
import com.dims_hsrc.common.core.controller.BaseController;
import com.dims_hsrc.common.core.domain.AjaxResult;
import com.dims_hsrc.common.core.domain.entity.SysDictType;
import com.dims_hsrc.common.core.page.TableDataInfo;
import com.dims_hsrc.common.enums.BusinessType;
import com.dims_hsrc.common.utils.poi.ExcelUtil;
import com.dims_hsrc.system.service.ISysDictTypeService;

/**
 * 数据字典信息
 *
 * @author 赵浩然
 */
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController {
    @Autowired
    private ISysDictTypeService serv;

    @GetMapping("/list")
    public TableDataInfo list(SysDictType vo) {
        System.out.println(new Throwable().getStackTrace()[0]);
        startPage();
        List<SysDictType> list = serv.list(vo);
        TableDataInfo res = getDataTable(list);
        return res;
    }

    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictType vo) {
        System.out.println(new Throwable().getStackTrace()[0]);
        List<SysDictType> list = serv.list(vo);
        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
        util.exportExcel(response, list, "字典类型");
    }

    /**
     * 查询字典类型详细
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return success(serv.findById(id));
    }

    /**
     * 新增字典类型
     */
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDictType vo) {
        System.out.println(new Throwable().getStackTrace()[0]);
        if (!serv.checkUnique(vo)) {
            return error("新增字典'" + vo.getDictName() + "'失败，字典类型已存在");
        }
        vo.setCreateBy(getUsername());
        vo.setUpdateBy(getUsername());
        return toAjax(serv.add(vo));
    }

    /**
     * 修改字典类型
     */
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult update(@Validated @RequestBody SysDictType vo) {
        System.out.println(new Throwable().getStackTrace()[0]);
        if (!serv.checkUnique(vo)) {
            return error("修改字典'" + vo.getDictName() + "'失败，字典类型已存在");
        }
        vo.setUpdateBy(getUsername());
        return toAjax(serv.update(vo));
    }

    /**
     * 删除字典类型
     */
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        System.out.println(new Throwable().getStackTrace()[0]);
        serv.remove(ids);
        return success();
    }

    /**
     * 刷新字典缓存
     */
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public AjaxResult refreshCache() {
        System.out.println(new Throwable().getStackTrace()[0]);
        serv.resetCache();
        return success();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect() {
        System.out.println(new Throwable().getStackTrace()[0]);
        List<SysDictType> dictTypes = serv.listAll();
        return success(dictTypes);
    }
}
